package com.example.bailianagent.controller;

import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgent;
import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgentOptions;
import com.alibaba.cloud.ai.dashscope.api.DashScopeAgentApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/*非流式调用*/
@Slf4j
@RestController
@RequestMapping("/ai")
public class BaiLianAgentController {
    //创建DashScopeAgent对象，用于和百炼大模型进行交互
    private DashScopeAgent agent;
    @Value("${spring.ai.dashscope.agent.options.app-id}")
    private String apppId;

    //构造方法注入
    public BaiLianAgentController(DashScopeAgentApi dashScopeAgentApi) {
        this.agent = new DashScopeAgent(dashScopeAgentApi);
    }
    @GetMapping("/bailian/agent/call")
    public String call(String message){
        log.debug("调用百炼大模型返回结果：response={}",message);
        ChatResponse response = agent.call(new Prompt(message, DashScopeAgentOptions.builder().withAppId(apppId).build()));
        log.debug("调用百炼大模型返回结果：response={}",response);
        String content =response.getResult().getOutput().getText();
        log.debug("从返回结果中提取文本答案：content={}",content);
        return content;
    }
}
